/*
设备(蓝牙)信息通知
单例,支持多页面监听
 */
//socket设备连接状态


//蓝牙设备连接状态
import 'package:xsdk_flutter/xsdk/utils/bluetooth/api/x_ble_connect_api.dart';
import 'package:xsdk_flutter/xsdk/utils/queue/x_bean_queue_message.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_log.dart';

typedef XFunctionBleDeviceConnectListener<T> = void Function(
    bool isConnect,
    String deviceAddress
    );
//接收蓝牙设备消息
typedef XFunctionReceiveBleDeviceMessageListener<T> = void Function(
    XBleConnectApi conn,
    String deviceAddress,
    String characteristicId,//适用ble
    List<int> msgBytes,//原始数据
    String msg,//对应原始数据的字符串
    );
//写入到蓝牙设备状态
typedef XFunctionWriteMessageToBleDeviceListener<T> = void Function(
    bool isSuccess,
    String? deviceName,
    String? deviceAddress,
    XBaseQueueMessage? msg
    );


class XBluetoothDeviceNotifyCallback{

  XBluetoothDeviceNotifyCallback._();

  static final XBluetoothDeviceNotifyCallback _instance= XBluetoothDeviceNotifyCallback._();
  static XBluetoothDeviceNotifyCallback get instance => _instance;


  //支持多页面蓝牙监听//////////////////////////////////////////////////////////////////////

  //添加BLE蓝牙设备连接状态监听
  void addBleDeviceConnectListener(XFunctionBleDeviceConnectListener? listener){
    if(listener!=null){
      _listBleDeviceConnectListeners.add(listener);
    }
  }
  //删除BLE蓝牙设备连接状态监听
  void removeBleDeviceConnectListener(XFunctionBleDeviceConnectListener? listener){
    if(listener!=null){
      _listBleDeviceConnectListeners.remove(listener);
    }else{
      _listBleDeviceConnectListeners.clear();
    }

  }

  //添加 写入内容到BLE蓝牙设备状态监听
  void addWriteMessageToBleDeviceListener(XFunctionWriteMessageToBleDeviceListener? listener){
    if(listener!=null){
      _listWriteMessageToBleDeviceListeners.add(listener);
    }
  }
  //删除 写入内容到BLE蓝牙设备状态监听
  void removeWriteMessageToBleDeviceListener(XFunctionWriteMessageToBleDeviceListener? listener){
    if(listener!=null){
      _listWriteMessageToBleDeviceListeners.remove(listener);
    }else{
      _listWriteMessageToBleDeviceListeners.clear();
    }

  }

  //添加 接收蓝牙BLE设备消息监听
  void addReceiveBleDeviceMessageListener(XFunctionReceiveBleDeviceMessageListener? listener){
    if(listener!=null){
      _listReceiveBleDeviceMessageListeners.add(listener);
    }

  }
  //删除 接收蓝牙BLE设备消息监听
  void removeReceiveBleDeviceMessageListener(XFunctionReceiveBleDeviceMessageListener? listener){
    if(listener!=null){
      _listReceiveBleDeviceMessageListeners.remove(listener);
    }else{
      _listReceiveBleDeviceMessageListeners.clear();
    }
  }



  //私有/////////////////////////////////////////////////////////////////////////

  //BLE蓝牙设备连接状态监听
  final List<XFunctionBleDeviceConnectListener> _listBleDeviceConnectListeners=[];
  //写入内容到BLE蓝牙设备状态监听
  final List<XFunctionWriteMessageToBleDeviceListener> _listWriteMessageToBleDeviceListeners=[];
  //接收蓝牙BLE设备消息监听
  final List<XFunctionReceiveBleDeviceMessageListener> _listReceiveBleDeviceMessageListeners=[];



  //XSDK内部调用, BLE设备 //////////////////////////////////////////////////////////////////////
  //蓝牙BLE设备实时连接状态回调
  void bleDeviceConnectStatusCallback({
    required bool isConnect,
    required String bluetoothAddress,
  }){

    XLogUtils.printLog('---XDeviceNotifyCallback 蓝牙设备实时连接状态: isConnect=$isConnect, bluetoothAddress=$bluetoothAddress',tag: 'XOther');
    try{
      for(XFunctionBleDeviceConnectListener listener in _listBleDeviceConnectListeners){
        listener(isConnect,bluetoothAddress);
      }
    }catch(e){
      //
    }

  }

  //接收蓝牙BLE设备消息回调
  void receiveBleDeviceMsgCallback({
    required XBleConnectApi conn,
    required String bluetoothAddress,
    required String characteristicId,//适用ble
    required List<int> msgBytes,//原始数据
    required String msg,//对应原始数据的字符串
  }){

    XLogUtils.printLog('---XDeviceNotifyCallback 收到蓝牙设备消息: bluetoothAddress=$bluetoothAddress, characteristicId=$characteristicId, msg=$msg');

    try{
      for(XFunctionReceiveBleDeviceMessageListener listener in _listReceiveBleDeviceMessageListeners){
        listener(conn,bluetoothAddress,characteristicId,msgBytes,msg);
      }
    }catch(e){
      //
    }
  }

  //写入消息到蓝牙BLE设备结果回调
  void writeMsgToBleDeviceCallback({
    required bool isSuccess,
    String? deviceName,
    String? bluetoothAddress,
    XBaseQueueMessage? msg,
  }){
    XLogUtils.printLog('---XDeviceNotifyCallback 写入蓝牙设备结果: isSuccess=$isSuccess, deviceAddress=$bluetoothAddress');

    try{
      for(XFunctionWriteMessageToBleDeviceListener listener in _listWriteMessageToBleDeviceListeners){
        listener(isSuccess,deviceName,bluetoothAddress,msg);
      }
    }catch(e){
      //
    }
  }




}